package org.metasketch.editor.perspectives;

import org.metasketch.editor.Editor;
import org.metasketch.editor.Project;
import org.metasketch.editor.diagrams.DiagramManager;
import org.metasketch.editor.project.CommonProject;
import org.metasketch.editor.toolbox.DiagramTypeCreator;
import org.metasketch.editor.toolbox.ToolboxViewer;
import org.metasketch.editor.toolbox.advanced.ModelViewer;
import org.metasketch.javafx.graphics.DiagramView;

public class BaseLayout implements ILayout {

	protected BaseDiagrammaticPerspective perspective;
	protected DiagramManager diagramManager;
	// protected Editor editor;
	private ToolboxViewer toolbox;
	private DiagramView diagramViewer;
	protected ModelViewer modelViewer;

	private DiagramTypeCreator diagramTypeCreator;

	public BaseLayout(BaseDiagrammaticPerspective perspective) {
		this.perspective = perspective;

	}

	@Override
	public void apply(Object editorO) {
		// this.editor = editor;
		Editor editor = (Editor) editorO;
		setup(editor);

	}

	protected void create() {
		// diagramManager = new DiagramManager(
		// perspective.getDiagramTypeManager(), editor.getProject());

		diagramManager = new DiagramManager(
				perspective.getDiagramTypeManager(), null);

		toolbox = new ToolboxViewer();
		diagramManager.setToolboxViewer(toolbox);

		diagramViewer = new DiagramView();
		diagramManager.setDiagramViewer(diagramViewer);

		diagramTypeCreator = new DiagramTypeCreator(diagramManager);

		modelViewer = new ModelViewer();
		modelViewer.setScripts(perspective.getModelViewerScripts());
		diagramManager.setModelViewer(modelViewer);

	}

	protected void setup(Editor editor) {

		// create();

		// diagramManager.setProject((DEMOProject) editor.getProject());

		editor.getLeft().setTop(toolbox);

		editor.getCenter().setCenter(diagramViewer);

		editor.getRight().setTop(diagramTypeCreator);
		editor.getRight().setCenter(modelViewer);

		// editor.getRight().requestLayout();
		// toolbox.requestFocus();

		// editor.refreshTitle();

		// setup(editor.getProject());

	}

	public void removeFromEditor() {

	}

	public Perspective getPerspective() {
		return perspective;
	}

	// @Override
	// public void setProject(Editor editor, Project project) {
	// setup(editor);
	//
	// }

	@Override
	public void setup(Project project) {
		create();

		try {
			diagramManager.setProject((CommonProject) project);
			modelViewer.setModel(((CommonProject) project).getModel());
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
